﻿using System;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Element.Conditional;
using RAP.Framework.Libary.DomTemplateBuilder.Handler.Conditional;

namespace RAP.Framework.Libary.DomTemplateBuilder.Element.Switch
{
	/// <summary>
	/// Switch Case元素基类
	/// </summary>
	public class SwitchElementBase : CompareBase
    {
		public virtual string ConditionHandlerName
		{
			get
			{
				return this.ToString ( "ConditionType", "IsEqual" );
			}
			set
			{
				this[ "ConditionType" ] = value;
				_conditionHandlerType = null;
				_conditionHandler = null;
			}
		}

		protected Type _conditionHandlerType = null;
		/// <summary>
		/// 对应CompareHandler的类型
		/// </summary>
		public virtual Type ConditionHandlerType
		{
			get
			{
				if ( _conditionHandlerType == null )
				{
					_conditionHandlerType = this.Template.DomTemplate.GetHandlerType ( this.ConditionHandlerName );
				}
				return _conditionHandlerType;
			}
			set
			{
				_conditionHandlerType = value;
			}
		}

		protected ConditionalHandler _conditionHandler = null;
		/// <summary>
		/// 获取当前节点对应的Handler
		/// </summary>
		public virtual ConditionalHandler ConditionHandler
		{
			get
			{
				if ( _conditionHandler != null )
				{
					return _conditionHandler;
				}
				if ( this.HandlerType == null )
				{
					return null;
				}
				_conditionHandler = this.HandlerType.Assembly.CreateInstance ( this.ConditionHandlerType.FullName ) as ConditionalHandler;
				_conditionHandler.Element = this;
				return _conditionHandler;
			}
		}
    }
}
